import {
	defineStore
} from "pinia"

import {
	ref
} from "vue"

import {
	departmentListApi,
	preOrderApi,
	createOrderApi
} from "@/api/modules/consult.js"

import {
	patientDetailApi
} from "@/api/modules/patient.js"

const useConsultStore = defineStore("consult", () => {

	const illnessValue = ref({
		illnessDesc: '',
		illnessTime: '',
		consultFlag: '',
		pictures: []
	});

	const illnessInfo = ref({
		...illnessValue.value
	})

	const type = ref("")
	const illnessType = ref("")
	const depId = ref("")

	const patientId = ref("")

	const departmentList = ref([])

	const patientDetail = ref({})

	const preOrderData = ref({})

	// 订单id
	const orderId = ref("")


	const departmentListAction = async () => {
		const res = await departmentListApi()
		if (res.code !== 10000) return uni.utils.toast(res.message)
		console.log(res);
		departmentList.value = res.data
	}

	const preOrderApiAction = async () => {
		const res = await preOrderApi(type.value)
		if (res.code !== 10000) return uni.utils.toast(res.message)
		preOrderData.value = res.data
		console.log(res);
	}

	/* 获取患者详情信息 */
	const consultDetailApiAction = async (id) => {

		// 防止订单重复创建
		if (orderId.value !== "") return uni.utils.toast("订单不能重复创建!")
		const {
			code,
			data,
			message
		} = await patientDetailApi(patientId.value);
		if (code !== 10000) return uni.utils.toast(message)
		console.log(data);
		patientDetail.value = data
		return data
	}

	const createOrderApiAction = async (data) => {
		const res = await createOrderApi(data)
		if (code !== 10000) return uni.utils.toast(message)
		orderId.value = res.data.id

		tyoe.value = ""
		illnessInfo.value = illnessValue.value
		illnessType.value = ""
		patientId.value = ""
		depId.value = ""
		console.log(res);
	}

	return {
		illnessValue,
		illnessInfo,
		type,
		illnessType,
		depId,
		patientId,
		departmentList,
		preOrderData,
		patientDetail,
		departmentListAction,
		preOrderApiAction,
		consultDetailApiAction,
		createOrderApiAction
	}
}, {
	persist: {
		paths: ["illnessInfo", "type", "illnessType", "depId", "patientId"]
	},
})


export default useConsultStore